/**
 * @desc 价格公共组件
 * @props  {string or number} price 价格值
 * @props  {string} type    乘除类型 乘：multiply  除：divide
 * @props  {string} className     样式名称
 * @props  {string} symbol   价格前面的符号 默认为￥
 * @props  {string}    价格保留小数位数 默认为 0
 */
import React from 'react'
import PropTypes from 'prop-types'
import { transNumber } from "@/utils/numberTrans";

class TheMoney extends React.Component {
    formatting = (s) => {
        let intSum = s.substring(0, s.indexOf(".")).replace(/\B(?=(?:\d{3})+$)/g, ',');//取到整数部分
        let dot = s.substring(s.length, s.indexOf("."))//取到小数部分搜索
        return intSum + dot
    }

    render() {
        const { price, type, className, symbol, numFixed, color } = this.props;
        const params =price!==null? transNumber((Number.parseFloat(price) / 100), numFixed):''
        return (
            <div style={{ display: 'inline-block', color: color ? color : '#000' }} className={className}>
                {   price !== null && <span>{symbol}</span> }
                {
                    type === 'divide' &&
                    <span>{this.formatting(params)}</span>
                }
                {
                    type === 'multiply' &&
                    <span>{this.formatting(params)}</span>
                }
            </div>
        )
    }
}

TheMoney.propTypes = {
    price: PropTypes.number || PropTypes.string,
    type: PropTypes.string,
    numFixed: PropTypes.number,
    symbol: PropTypes.string
}
TheMoney.defaultProps = {
    price: 0,
    type: 'divide',
    symbol: '￥',
    numFixed: 2
}
export default TheMoney
